• Jump To … +
    zoo_frontend/node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.cjs.js zoo_frontend/node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.js zoo_frontend/node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.min.js zoo_frontend/node_modules/hoist-non-react-statics/src/index.js zoo_frontend/pages/_app.jsx zoo_frontend/pages/_document.jsx zoo_frontend/pages/admin/delivery-containers/index.js zoo_frontend/pages/admin/department/index.js zoo_frontend/pages/admin/group-diets/index.js zoo_frontend/pages/admin/species/index.js zoo_frontend/pages/admin/user/index.js zoo_frontend/pages/diet/edit.js zoo_frontend/pages/diet/index.js zoo_frontend/pages/diet/new.js zoo_frontend/pages/food/dataSrc.js zoo_frontend/pages/food/edit.js zoo_frontend/pages/food/index.js zoo_frontend/pages/food/new.js zoo_frontend/pages/food/nicknames.js zoo_frontend/pages/food/nutrDef.js zoo_frontend/pages/food/units.js zoo_frontend/pages/home/index.js zoo_frontend/pages/index.jsx zoo_frontend/pages/kitchen/index.js zoo_frontend/pages/kitchen/prep/index.js zoo_frontend/pages/login/index.js zoo_frontend/pages/nutritionist/index.js zoo_frontend/pages/print/bin-label.js zoo_frontend/pages/print/index.js zoo_frontend/pages/print/labels.js zoo_frontend/pages/print/prep-sheet.js zoo_frontend/pages/profile/index.js zoo_frontend/pages/reports/cost-by-gl-code.js zoo_frontend/pages/reports/dept-cards.js zoo_frontend/pages/reports/dept-keeper-cards.js zoo_frontend/pages/reports/feeding-cost.js zoo_frontend/pages/reports/index.js zoo_frontend/pages/reports/prep-cards-table.js zoo_frontend/src/api/Animals.js zoo_frontend/src/api/Api.js zoo_frontend/src/api/BudgetIds.js zoo_frontend/src/api/CaseNotes.js zoo_frontend/src/api/DataSrc.js zoo_frontend/src/api/DeliveryContainers.js zoo_frontend/src/api/Departments.js zoo_frontend/src/api/DietChanges.js zoo_frontend/src/api/DietHistory.js zoo_frontend/src/api/DietPlans.js zoo_frontend/src/api/Diets.js zoo_frontend/src/api/Food.js zoo_frontend/src/api/FoodCategories.js zoo_frontend/src/api/FoodPrepTables.js zoo_frontend/src/api/FoodWeights.js zoo_frontend/src/api/LifeStages.js zoo_frontend/src/api/Locations.js zoo_frontend/src/api/NutData.js zoo_frontend/src/api/NutrDef.js zoo_frontend/src/api/PrepNotes.js zoo_frontend/src/api/RoleMappings.js zoo_frontend/src/api/Roles.js zoo_frontend/src/api/Species.js zoo_frontend/src/api/Subenclosures.js zoo_frontend/src/api/Units.js zoo_frontend/src/api/Users.js zoo_frontend/src/api/index.js zoo_frontend/src/components/ConfirmationDialog.jsx zoo_frontend/src/components/ErrorPage.jsx zoo_frontend/src/components/FormCheckbox.jsx zoo_frontend/src/components/Header.jsx zoo_frontend/src/components/KitchenView.jsx zoo_frontend/src/components/Notifications.jsx zoo_frontend/src/components/PrintPrepSheets/PrepSheetPrintOut.jsx zoo_frontend/src/components/PrintPrepSheets/PrintPrepSheets.jsx zoo_frontend/src/components/PrintPrepSheets/index.js zoo_frontend/src/components/ReactSingleSelect.jsx zoo_frontend/src/components/SidebarDrawer.jsx zoo_frontend/src/components/VirtualTable.jsx zoo_frontend/src/components/index.js zoo_frontend/src/getPageContext.js zoo_frontend/src/pages/PageAccess.js zoo_frontend/src/pages/admin/deliveryContainers/deliveryContainers.jsx zoo_frontend/src/pages/admin/deliveryContainers/deliveryContainers.styles.js zoo_frontend/src/pages/admin/deliveryContainers/index.js zoo_frontend/src/pages/admin/department/department.jsx zoo_frontend/src/pages/admin/department/department.styles.js zoo_frontend/src/pages/admin/department/index.js zoo_frontend/src/pages/admin/groupDiets/groupDiets.jsx zoo_frontend/src/pages/admin/groupDiets/groupDiets.styles.js zoo_frontend/src/pages/admin/groupDiets/index.js zoo_frontend/src/pages/admin/species/index.js zoo_frontend/src/pages/admin/species/species.jsx zoo_frontend/src/pages/admin/species/species.styles.js zoo_frontend/src/pages/admin/user/index.js zoo_frontend/src/pages/admin/user/user.jsx zoo_frontend/src/pages/admin/user/user.styles.js zoo_frontend/src/pages/diet/CaseNotesForm.jsx zoo_frontend/src/pages/diet/CurrentDiet.jsx zoo_frontend/src/pages/diet/DietChangeCard.jsx zoo_frontend/src/pages/diet/DietHistory.jsx zoo_frontend/src/pages/diet/DietPlanChangeDialog.jsx zoo_frontend/src/pages/diet/DietSelectDialog.jsx zoo_frontend/src/pages/diet/diet.jsx zoo_frontend/src/pages/diet/diet.styles.js zoo_frontend/src/pages/diet/dietForm.jsx zoo_frontend/src/pages/diet/dietHistoryList.jsx zoo_frontend/src/pages/diet/edit/edit.jsx zoo_frontend/src/pages/diet/edit/edit.styles.js zoo_frontend/src/pages/diet/edit/index.js zoo_frontend/src/pages/diet/index.js zoo_frontend/src/pages/diet/new/index.js zoo_frontend/src/pages/diet/new/new.jsx zoo_frontend/src/pages/diet/new/new.styles.js zoo_frontend/src/pages/diet/prepNotesForm.jsx zoo_frontend/src/pages/food/dataSrc/dataSrc.jsx zoo_frontend/src/pages/food/dataSrc/dataSrc.styles.js zoo_frontend/src/pages/food/dataSrc/index.js zoo_frontend/src/pages/food/edit/FoodWeightTable.jsx zoo_frontend/src/pages/food/edit/edit.jsx zoo_frontend/src/pages/food/edit/edit.styles.js zoo_frontend/src/pages/food/edit/index.js zoo_frontend/src/pages/food/food.jsx zoo_frontend/src/pages/food/food.styles.js zoo_frontend/src/pages/food/foodForm.jsx zoo_frontend/src/pages/food/index.js zoo_frontend/src/pages/food/new/index.js zoo_frontend/src/pages/food/new/new.jsx zoo_frontend/src/pages/food/new/new.styles.js zoo_frontend/src/pages/food/nicknames/index.js zoo_frontend/src/pages/food/nicknames/nicknames.jsx zoo_frontend/src/pages/food/nicknames/nicknames.styles.js zoo_frontend/src/pages/food/nutrDef/index.js zoo_frontend/src/pages/food/nutrDef/nutrDef.jsx zoo_frontend/src/pages/food/nutrDef/nutrDef.styles.js zoo_frontend/src/pages/food/units/index.js zoo_frontend/src/pages/food/units/units.jsx zoo_frontend/src/pages/food/units/units.styles.js zoo_frontend/src/pages/home/home.jsx zoo_frontend/src/pages/home/home.styles.js zoo_frontend/src/pages/home/index.js zoo_frontend/src/pages/kitchen/index.js zoo_frontend/src/pages/kitchen/kitchenHome.jsx zoo_frontend/src/pages/kitchen/kitchenHome.styles.js zoo_frontend/src/pages/kitchen/prep/index.js zoo_frontend/src/pages/kitchen/prep/kitchen.jsx zoo_frontend/src/pages/kitchen/prep/kitchen.styles.js zoo_frontend/src/pages/login/index.js zoo_frontend/src/pages/login/login.jsx zoo_frontend/src/pages/login/login.styles.js zoo_frontend/src/pages/nutritionist/admin.jsx zoo_frontend/src/pages/nutritionist/admin.styles.js zoo_frontend/src/pages/nutritionist/index.js zoo_frontend/src/pages/print/admin.jsx zoo_frontend/src/pages/print/admin.styles.js zoo_frontend/src/pages/print/bin-label/admin.jsx zoo_frontend/src/pages/print/bin-label/admin.styles.js zoo_frontend/src/pages/print/bin-label/index.js zoo_frontend/src/pages/print/index.js zoo_frontend/src/pages/print/labels/admin.jsx zoo_frontend/src/pages/print/labels/admin.styles.js zoo_frontend/src/pages/print/labels/index.js zoo_frontend/src/pages/print/prep-sheet/admin.jsx zoo_frontend/src/pages/print/prep-sheet/admin.styles.js zoo_frontend/src/pages/print/prep-sheet/index.js zoo_frontend/src/pages/profile/index.js zoo_frontend/src/pages/profile/profile.jsx zoo_frontend/src/pages/profile/profile.styles.js zoo_frontend/src/pages/reports/admin.jsx zoo_frontend/src/pages/reports/admin.styles.js zoo_frontend/src/pages/reports/cost-by-gl-code/admin.jsx zoo_frontend/src/pages/reports/cost-by-gl-code/admin.styles.js zoo_frontend/src/pages/reports/cost-by-gl-code/index.js zoo_frontend/src/pages/reports/dept-cards/admin.jsx zoo_frontend/src/pages/reports/dept-cards/admin.styles.js zoo_frontend/src/pages/reports/dept-cards/index.js zoo_frontend/src/pages/reports/dept-keeper-cards/admin.jsx zoo_frontend/src/pages/reports/dept-keeper-cards/admin.styles.js zoo_frontend/src/pages/reports/dept-keeper-cards/index.js zoo_frontend/src/pages/reports/feeding-cost/admin.jsx zoo_frontend/src/pages/reports/feeding-cost/admin.styles.js zoo_frontend/src/pages/reports/feeding-cost/index.js zoo_frontend/src/pages/reports/index.js zoo_frontend/src/pages/reports/prep-cards-table/admin.jsx zoo_frontend/src/pages/reports/prep-cards-table/admin.styles.js zoo_frontend/src/pages/reports/prep-cards-table/index.js zoo_frontend/src/static/LocalStorage.js zoo_frontend/src/static/Roles.js zoo_frontend/src/util/AuthProvider.jsx zoo_frontend/src/util/PageLayout.jsx zoo_frontend/src/util/TableColumnHelper.js zoo_frontend/src/util/WithPropsChecker.jsx zoo_frontend/src/util/camelToNorm.js zoo_frontend/src/util/withAuth.jsx
  • foodForm.jsx

  • ¶
    /* eslint-disable react/jsx-no-bind */
    import React, { Fragment } from 'react';
    import Button from '@material-ui/core/Button';
    import TextField from '@material-ui/core/TextField';
    import { Grid, LinearProgress } from '@material-ui/core';
    import PropTypes from 'prop-types';
    import { withFormik, Field } from 'formik';
    import Select from 'react-select';
    import * as Yup from 'yup';
    
    import { FormCheckbox, SingleSelect } from '../../components';
    
    export const DisplayFormikState = props => (
      <div style={{ margin: '1rem 0' }}>
        <pre
          style={{
            background: '#f6f8fa',
            fontSize: '.65rem',
            padding: '.5rem',
          }}
        >
          <strong>props</strong> ={' '}
          {JSON.stringify(props, null, 2)}
        </pre>
      </div>
    );
    
    const formikEnhancer = withFormik({
      validationSchema: Yup.object().shape({
        food: Yup.string().required('Name is required'),
        sciName: Yup.string().nullable(),
        manufacturerName: Yup.string().nullable(),
        ohdzName: Yup.string().nullable(),
        costG: Yup.number().nullable(),
        budgetId: Yup.string().required('Budget ID is required'),
        category: Yup.string().required('Food Category is required'),
        usdaFoodGroupDesc: Yup.number().nullable(),
        dry: Yup.bool(),
        meat: Yup.bool(),
        preChop: Yup.bool(),
        preBag: Yup.bool(),
        active: Yup.bool(),
      }),
      mapPropsToValues: props => ({
        food: props.food ? props.food : '',
        sciName: props.sciName ? props.sciName : '',
        manufacturerName: props.manufacturerName ? props.manufacturerName : '',
        ohdzName: props.ohdzName ? props.ohdzName : '',
        costG: props.costG ? props.costG : 0,
        budgetId: props.budgetId,
        category: props.category,
        usdaFoodGroupDesc: props.usdaFoodGroupDesc,
        dry: props.dry === 1,
        meat: props.meat === 1,
        preChop: props.preChop === 1,
        preBag: props.preBag === 1,
        active: props.active === 1,
        submitForm: props.submitForm, // jank way to send in the function
      }),
      handleSubmit: (values, { setSubmitting }) => {
        const {
          dry, meat, preChop, preBag, active, submitForm, ...rest
        } = values;
        const payload = {
          dry: dry ? 1 : 0,
          meat: meat ? 1 : 0,
          preChop: preChop ? 1 : 0,
          preBag: preBag ? 1 : 0,
          active: active ? 1 : 0,
          ...rest,
        };
        submitForm(payload).then(() => {
          setSubmitting(false);
        }, () => {
          setSubmitting(false);
        });
      },
      displayName: 'MyForm',
      enableReinitialize: true,
    });
    
    
    const Form = props => {
      const {
        values: {
          sciName, manufacturerName, ohdzName, food, costG, budgetId, category, usdaFoodGroupDesc, dry, meat, preChop, preBag, active,
        },
        errors,
        touched,
        handleChange,
        isValid,
        setFieldTouched,
        setFieldValue,
        isSubmitting,
      } = props;
    
      const change = (name, e) => {
        e.persist();
        handleChange(e);
        setFieldTouched(name, true, false);
      };
    
      const handleBlur = (name, e) => {
        const { value } = e.target;
        if (name === 'food' && ohdzName === '') {
          if (value) {
            setFieldValue('ohdzName', value, false);
            setFieldTouched('ohdzName', true, false);
          }
        }
      };
      return (
        <form
          onSubmit={(e) => {
            e.preventDefault();
            props.handleSubmit(e);
          }}
        >
          <Grid container>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="food"
                name="food"
                helperText={touched.food ? errors.food : ''}
                error={touched.food && Boolean(errors.food)}
                label="Food Name"
                value={food}
                onChange={change.bind(null, 'food')}
                variant="outlined"
                fullWidth
                onFocus={change.bind(null, 'food')}
                onBlur={handleBlur.bind(null, 'food')}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="sciName"
                name="sciName"
                helperText={touched.sciName ? errors.sciName : ''}
                error={touched.sciName && Boolean(errors.sciName)}
                label="Sci Name"
                value={sciName}
                onChange={change.bind(null, 'sciName')}
                fullWidth
                variant="outlined"
                onFocus={change.bind(null, 'sciName')}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="manufacturerName"
                name="manufacturerName"
                helperText={touched.manufacturerName ? errors.manufacturerName : ''}
                error={touched.manufacturerName && Boolean(errors.manufacturerName)}
                label="Manufacturer Name"
                fullWidth
                variant="outlined"
                value={manufacturerName}
                onChange={change.bind(null, 'manufacturerName')}
                onFocus={change.bind(null, 'manufacturerName')}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="ohdzName"
                name="ohdzName"
                helperText={touched.ohdzName ? errors.ohdzName : ''}
                error={touched.ohdzName && Boolean(errors.ohdzName)}
                label="OHDZ Name"
                fullWidth
                variant="outlined"
                value={ohdzName}
                onChange={change.bind(null, 'ohdzName')}
                onFocus={change.bind(null, 'ohdzName')}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="costG"
                name="costG"
                helperText={touched.costG ? errors.costG : ''}
                error={touched.costG && Boolean(errors.costG)}
                label="Cost Per Gram"
                value={costG}
                onChange={change.bind(null, 'costG')}
                onFocus={change.bind(null, 'costG')}
                variant="outlined"
                fullWidth
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px', alignSelf: 'center' }}>
              <Field
                name="budgetId"
                label="Budget ID"
                component={SingleSelect}
                suggestions={props.budgetCodes}
                defaultValue={budgetId}
                error={touched.budgetId && Boolean(errors.budgetId)}
                helperText={touched.budgetId ? errors.budgetId : ''}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px', alignSelf: 'center' }}>
              <Field
                name="category"
                label="Category"
                component={SingleSelect}
                suggestions={props.foodCategories}
                defaultValue={category}
                error={touched.category && Boolean(errors.category)}
                helperText={touched.category ? errors.category : ''}
              />
            </Grid>
            <Grid item xs={12} md={4} style={{ padding: '10px' }}>
              <TextField
                id="usdaFoodGroupDesc"
                name="usdaFoodGroupDesc"
                helperText={touched.usdaFoodGroupDesc ? errors.usdaFoodGroupDesc : ''}
                error={touched.usdaFoodGrouDesc && Boolean(errors.usdaFoodGroupDesc)}
                label="USDA Food Group Description"
                value={usdaFoodGroupDesc}
                onChange={change.bind(null, 'usdaFoodGroupDesc')}
                onFocus={change.bind(null, 'usdaFoodGroupDesc')}
                variant="outlined"
                fullWidth
              />
            </Grid>
          </Grid>
          <Grid container>
            <Grid item xs={6} md={2} style={{ padding: '10px' }}>
              <FormCheckbox
                id="dry"
                name="dry"
                helperText={touched.dry ? errors.dry : ''}
                error={touched.active && Boolean(errors.dry)}
                label="Dry"
                value={dry}
                onChange={(e) => {
                  e.target.value = e.target.checked;
                  change.bind(null, 'dry')(e);
                }}
                variant="outlined"
                fullWidth
              />
            </Grid>
            <Grid item xs={6} md={2} style={{ padding: '10px' }}>
              <FormCheckbox
                id="meat"
                name="meat"
                helperText={touched.meat ? errors.meat : ''}
                error={touched.active && Boolean(errors.meat)}
                label="Meat"
                value={meat}
                onChange={(e) => {
                  e.target.value = e.target.checked;
                  change.bind(null, 'meat')(e);
                }}
                variant="outlined"
                fullWidth
              />
            </Grid>
            <Grid item xs={6} md={2} style={{ padding: '10px' }}>
              <FormCheckbox
                id="preChop"
                name="preChop"
                helperText={touched.preChop ? errors.preChop : ''}
                error={touched.active && Boolean(errors.preChop)}
                label="Pre Chop"
                value={preChop}
                onChange={(e) => {
                  e.target.value = e.target.checked;
                  change.bind(null, 'preChop')(e);
                }}
                variant="outlined"
                fullWidth
              />
            </Grid>
            <Grid item xs={6} md={2} style={{ padding: '10px' }}>
              <FormCheckbox
                id="preBag"
                name="preBag"
                helperText={touched.preBag ? errors.preBag : ''}
                error={touched.active && Boolean(errors.preBag)}
                label="Pre Bag"
                value={preBag}
                onChange={(e) => {
                  e.target.value = e.target.checked;
                  change.bind(null, 'preBag')(e);
                }}
                variant="outlined"
                fullWidth
              />
            </Grid>
            <Grid item xs={6} md={2} style={{ padding: '10px' }}>
              <FormCheckbox
                id="active"
                name="active"
                helperText={touched.active ? errors.active : ''}
                error={touched.active && Boolean(errors.active)}
                label="Active"
                value={active}
                onChange={(e) => {
                  e.target.value = e.target.checked;
                  change.bind(null, 'active')(e);
                }}
                variant="outlined"
                fullWidth
              />
            </Grid>
          </Grid>
          {isSubmitting &&
            <Fragment>
              <br />
              <LinearProgress />
              <br />
            </Fragment>
          }
          <Grid item xs={12} md={3} style={{ padding: '10px' }}>
            <Button
              type="submit"
              fullWidth
              variant="contained"
              color="primary"
              disabled={!isValid}
            >
              {props.submitButtonText}
            </Button>
          </Grid>
          {/* <DisplayFormikState {...props} /> */}
        </form>
      );
    };
    
    Form.propTypes = {
      values: PropTypes.shape({
        sciName: PropTypes.string,
        manufacturerName: PropTypes.string,
        ohdzName: PropTypes.string,
        food: PropTypes.string.isRequired,
        costG: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        budgetId: PropTypes.number,
        category: PropTypes.number,
        usdaFoodGroupDesc: PropTypes.string,
        dry: PropTypes.bool.isRequired,
        meat: PropTypes.bool.isRequired,
        preChop: PropTypes.bool.isRequired,
        preBag: PropTypes.bool.isRequired,
        active: PropTypes.bool.isRequired,
      }),
      errors: PropTypes.object.isRequired,
      touched: PropTypes.object.isRequired,
      handleChange: PropTypes.func.isRequired,
      isValid: PropTypes.bool.isRequired,
      setFieldTouched: PropTypes.func.isRequired,
      setFieldValue: PropTypes.func.isRequired,
      handleSubmit: PropTypes.func.isRequired,
      budgetCodes: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string,
        value: PropTypes.number,
      })).isRequired,
      foodCategories: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string,
        value: PropTypes.number,
      })).isRequired,
      isSubmitting: PropTypes.bool,
      submitButtonText: PropTypes.string,
    };
    
    Form.defaultProps = {
      values: {
        sciName: '',
        manufacturerName: '',
        ohdzName: '',
        food: '',
        costG: '',
        budgetId: 0,
        category: 0,
        usdaFoodGroupDesc: '',
        dry: false,
        meat: false,
        preChop: false,
        preBag: false,
        active: false,
      },
      isSubmitting: false,
      submitButtonText: 'Submit',
    };
    
    class FormikSelect extends React.Component {
      handleChange = value => {
  • ¶

    this is going to call setFieldValue and manually update values.topcis

        this.props.onChange('topics', value);
      };
    
      handleBlur = () => {
  • ¶

    this is going to call setFieldTouched and manually update touched.topcis

        this.props.onBlur('topics', true);
      };
    
      render() {
        return (
          <div style={{ margin: '1rem 0' }}>
            {/* eslint-disable jsx-a11y/label-has-for */}
            <label htmlFor="color">{this.props.label}</label>
            <Select
              id={this.props.id}
              options={this.props.options}
              onChange={this.handleChange}
              onBlur={this.handleBlur}
              value={this.props.value}
            />
            {!!this.props.error &&
              this.props.touched && (
                <div style={{ color: 'red', marginTop: '.5rem' }}>{this.props.error}</div>
            )}
          </div>
        );
      }
    }
    
    FormikSelect.propTypes = {
      onChange: PropTypes.func.isRequired,
      onBlur: PropTypes.func.isRequired,
      error: PropTypes.string.isRequired,
      touched: PropTypes.bool.isRequired,
      label: PropTypes.string,
      id: PropTypes.string.isRequired,
      value: PropTypes.shape({
        label: PropTypes.string.isRequired,
        value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
      }).isRequired,
      options: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string.isRequired,
        value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
      })).isRequired,
    };
    
    FormikSelect.defaultProps = {
      label: '',
    };
    
    export default formikEnhancer(Form);